﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace HttpRequest.Helper
{
    internal class EncryptHelper
    {
        private const string Key = @")O[NB]6,YF}+efca";

        /// <summary>
        /// DES算法解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <returns></returns>
        public string Decrypt(string toDecrypt)
        {
            try
            {
                byte[] keyArray = Encoding.UTF8.GetBytes(Key);
                byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception e)
            {
                throw;
            }
        }
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <returns></returns>
        public string Encrypt(string toEncrypt)
        {
            try
            {
                byte[] keyArray = Encoding.UTF8.GetBytes(Key);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception e)
            {
                throw;
            }

        }

        public string DesDecrypt(string input, string key)
        {
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            //using (var memoryStream = new MemoryStream())
            //{
            //    tripleDES.Key = keyBytes;
            //    //tripleDES.GenerateIV();
            //    using (var cryptoStream = new CryptoStream(memoryStream, tripleDES.CreateEncryptor(), CryptoStreamMode.Write))
            //    {
            //        byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
            //        cryptoStream.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
            //        cryptoStream.FlushFinalBlock();
            //        byte[] encryptedBytes = memoryStream.ToArray();
            //        return Convert.ToBase64String(encryptedBytes);
            //    }
            //}
            byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);

            // Create a MemoryStream.
            //MemoryStream mStream = new MemoryStream();
            //TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
            //tdsp.Mode = CipherMode.ECB;
            //tdsp.Padding = PaddingMode.PKCS7;
            //// Create a CryptoStream using the MemoryStream 
            //// and the passed key and initialization vector (IV).
            //byte[] iv;
            //tripleDES.GenerateIV();
            //CryptoStream cStream = new CryptoStream(mStream,
            //    tdsp.CreateEncryptor(keyBytes,null),
            //    CryptoStreamMode.Write);
            //// Write the byte array to the crypto stream and flush it.
            //cStream.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
            //cStream.FlushFinalBlock();
            //// Get an array of bytes from the 
            //// MemoryStream that holds the 
            //// encrypted data.
            //byte[] ret = mStream.ToArray();
            //// Close the streams.
            //cStream.Close();
            //mStream.Close();
            //// Return the encrypted buffer.
            //return Convert.ToBase64String(ret);
            //return ret;

            MemoryStream msDecrypt = new MemoryStream(bytesToBeEncrypted);
            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
            tdsp.Mode = CipherMode.ECB;
            tdsp.Padding = PaddingMode.PKCS7;
            // Create a CryptoStream using the MemoryStream 
            // and the passed key and initialization vector (IV).
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                tdsp.CreateDecryptor(keyBytes, null),
                CryptoStreamMode.Read);
            // Create buffer to hold the decrypted data.
            byte[] fromEncrypt = new byte[bytesToBeEncrypted.Length];
            // Read the decrypted data out of the crypto stream
            // and place it into the temporary buffer.
            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
            //Convert the buffer into a string and return it.
            //return fromEncrypt;
            return Convert.ToBase64String(fromEncrypt);

        }

        /// <summary>
        /// 数据压缩
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Compress(string source)
        {
            var bytes = Encoding.UTF8.GetBytes(source);
            using (var compressedStream = new MemoryStream())
            {
                using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
                {
                    zipStream.Write(bytes, 0, bytes.Length);
                }
                return Convert.ToBase64String(compressedStream.ToArray());
            }

        }
        /// <summary>
        /// 数据解压
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string Decompress(string result)
        {

            var bytes = Convert.FromBase64String(result);
            using (var compressStream = new MemoryStream(bytes))
            {
                using (var zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
                {
                    using (var resultStream = new MemoryStream())
                    {
                        zipStream.CopyTo(resultStream);
                        return Encoding.UTF8.GetString(resultStream.ToArray());

                    }
                }
            }
        }
    }
}
